# Copyright (c) 2024 - 2025 Kevin G. Schlosser

from micropython import const  # NOQA
import time

import lvgl as lv  # NOQA
import lcd_bus  # NOQA


# Tearing effect line on
_TEON = const(0x3500)

# Interface pixel format
_COLMOD = const(0x3A00)

# Memory Access Control
_MADCTL = const(0x3600)

# Sleep Out
_SLPOUT = const(0x1100)

# Display ON
_DISPON = const(0x2900)


def init1(self):
    param_buf = bytearray(52)
    param_mv = memoryview(param_buf)

    param_buf[:5] = bytearray([0x55, 0xAA, 0x52, 0x08, 0x01])
    self.set_params(0xF000, param_mv[:5])

    # AVDD Set AVDD 5.2V
    param_buf[:3] = bytearray([0x0D, 0x0D, 0x0D])
    self.set_params(0xB000, param_mv[:3])

    # AVDD ratio
    param_buf[:3] = bytearray([0x34, 0x34, 0x34])
    self.set_params(0xB600, param_mv[:3])

    # AVEE  -5.2V
    param_buf[:3] = bytearray([0x0D, 0x0D, 0x0D])
    self.set_params(0xB100, param_mv[:3])

    # AVEE ratio
    param_buf[:3] = bytearray([0x34, 0x34, 0x34])
    self.set_params(0xB700, param_mv[:3])

    # VCL  -2.5V
    param_buf[:3] = bytearray([0x00, 0x00, 0x00])
    self.set_params(0xB200, param_mv[:3])

    # VCL ratio
    param_buf[:3] = bytearray([0x24, 0x24, 0x24])
    self.set_params(0xB800, param_mv[:3])

    # VGH  15V
    param_buf[0] = 0x01
    self.set_params(0xBF00, param_mv[:1])

    param_buf[:3] = bytearray([0x0F, 0x0F, 0x0F])
    self.set_params(0xB300, param_mv[:3])

    # VGH  ratio
    param_buf[:3] = bytearray([0x34, 0x34, 0x34])
    self.set_params(0xB900, param_mv[:3])

    # VGL_REG  -10V
    param_buf[0] = 0x08
    self.set_params(0xB500, param_mv[:1])

    param_buf[:2] = bytearray([0x08, 0x08])
    self.set_params(0xB500, param_mv[:2])

    param_buf[0] = 0x03
    self.set_params(0xC200, param_mv[:1])

    # VGLX  ratio
    param_buf[:3] = bytearray([0x24, 0x24, 0x24])
    self.set_params(0xBA00, param_mv[:3])

    # VGMP/VGSP 4.5V/0V
    param_buf[:3] = bytearray([0x00, 0x78, 0x00])
    self.set_params(0xBC00, param_mv[:3])

    # VGMN/VGSN -4.5V/0V
    param_buf[:3] = bytearray([0x00, 0x78, 0x00])
    self.set_params(0xBD00, param_mv[:3])

    # VCOM  -1.325V
    param_buf[:2] = bytearray([0x00, 0x89])
    self.set_params(0xBE00, param_mv[:2])

    # Gamma Setting
    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD100, param_mv[:52])

    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD200, param_mv[:52])

    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD300, param_mv[:52])

    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD400, param_mv[:52])

    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD500, param_mv[:52])

    param_buf[:52] = bytearray([
        0x00, 0x2D, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x44, 0x00, 0x53, 0x00,
        0x88, 0x00, 0xB6, 0x00, 0xF3, 0x01, 0x22, 0x01, 0x64, 0x01, 0x92,
        0x01, 0xD4, 0x02, 0x07, 0x02, 0x08, 0x02, 0x34, 0x02, 0x5F, 0x02,
        0x78, 0x02, 0x94, 0x02, 0xA6, 0x02, 0xBB, 0x02, 0xCA, 0x02, 0xDB,
        0x02, 0xE8, 0x02, 0xF9, 0x03, 0x1F, 0x03, 0x7F])
    self.set_params(0xD600, param_mv[:52])

    # LV2 Page 0 enable
    param_buf[:5] = bytearray([0x55, 0xAA, 0x52, 0x08, 0x00])
    self.set_params(0xF000, param_mv[:5])

    # Display control
    param_buf[:2] = bytearray([0xCC, 0x00])
    self.set_params(0xB100, param_mv[:2])


def init2(self):
    param_buf = bytearray(5)
    param_mv = memoryview(param_buf)

    # Source hold time
    param_buf[0] = 0x05
    self.set_params(0xB600, param_mv[:1])

    # Set Gate EQ
    param_buf[:2] = bytearray([0x70, 0x70])
    self.set_params(0xB700, param_mv[:2])

    # Source EQ control (Mode 2)
    param_buf[:4] = bytearray([0x01, 0x03, 0x03, 0x03])
    self.set_params(0xB800, param_mv[:4])

    # INVERSION MODE
    param_buf[:3] = bytearray([0x02, 0x00, 0x00])
    self.set_params(0xBC00, param_mv[:3])

    # Timing control
    param_buf[:5] = bytearray([0xD0, 0x02, 0x50, 0x50, 0x50])
    self.set_params(0xC900, param_mv[:5])

    # Tearing effect line on
    param_buf[0] = 0x00
    self.set_params(_TEON, param_mv[:1])

    color_size = lv.color_format_get_size(self._color_space)
    if color_size == 2:  # NOQA
        pixel_format = 0x55
    elif color_size == 3:
        pixel_format = 0x77
    else:
        raise RuntimeError(f'color space not supported ({color_size})')

    param_buf[0] = pixel_format
    self.set_params(_COLMOD, param_mv[:1])

    # Memory access control
    param_buf[0] = self._madctl(
        self._color_byte_order,
        self._ORIENTATION_TABLE
    )
    self.set_params(_MADCTL, param_mv[:1])

    # Sleep out, no args, delay 120 ms
    self.set_params(_SLPOUT)
    time.sleep_ms(120)  # NOQA

    # Display on, no args, delay 120 ms
    self.set_params(_DISPON)
    time.sleep_ms(120)  # NOQA
